<template>
  <!-- 拒绝中标 -->
  <el-dialog
    v-model="props.visible"
    width="800px"
    :show-close="false"
    :close-on-click-modal="false"
    :before-close="handleClose"
  >
    <template #header="{ close, titleId, titleClass }">
      <div class="my-header">
        <h4
          :id="titleId"
          :class="titleClass"
        >
          {{ t('userCenter.t5') }}
        </h4>
        <img
          src="@/assets/img/icon_guanbi.png"
          @click="close"
          class="close"
        />
      </div>
    </template>

    <div>{{ t('userCenter.text3') }}</div>
    <div>{{ t('userCenter.text4') }}：</div>

    <div class="mt-1">
      <el-checkbox-group v-model="refusalReasonList">
        <el-checkbox
          :label="item.dictValue"
          v-for="item in dictData['refusal_reason']"
        >
          {{ item.dictLabel }}
        </el-checkbox>
      </el-checkbox-group>
    </div>

    <template #footer>
      <div class="dialog-footer flexcc">
        <el-button
          color="#252628"
          plain
          @click="cancel"
        >
          {{ t('cancelText') }}
        </el-button>
        <el-button
          type="primary"
          color="#252628"
          @click="confirm"
        >
          {{ t('confirmText') }}
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { onBeforeMount, reactive, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import useDict from '@/hooks/useDict'

import { refusalQuotationApi as dockOrderRefusalQuotationApi } from '@/service/dockOrder'
import { refusalQuotationApi as procurementRefusalQuotationApi } from '@/service/procurement'
import { refusalQuotationApi as externalAssistanceRefusalQuotationApi } from '@/service/externalAssistance'

const { dictData, getDictData } = useDict()
const { t, locale } = useI18n()

onBeforeMount(() => {
  init()
})

const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  // 报价单id
  objId: {
    type: [String, Number],
    required: true
  },
  // 1 2 3
  type: {
    type: [String, Number],
    default: 1
  }
})

const emit = defineEmits(['close', 'getList'])

const handleClose = () => {
  emit('close')
}

// 取消
const cancel = () => {
  handleClose()
}

// 确定
const confirm = () => {
  if (!refusalReasonList.value.length) {
    ElMessage.warning(t('userCenter.text4'))
    return
  }

  const obj = {
    1: {
      objTable: 'shcw_quotation_dock_order',
      apiFun: dockOrderRefusalQuotationApi
    },
    2: {
      objTable: 'shcw_quotation_procure_order',
      apiFun: procurementRefusalQuotationApi
    },
    3: {
      objTable: 'shcw_quotation_assist_order',
      apiFun: externalAssistanceRefusalQuotationApi
    }
  }

  const params = refusalReasonList.value.map((item) => {
    return {
      objId: props.objId,
      objTable: obj[props.type].objTable,
      refusalReason: item
    }
  })

  obj[props.type].apiFun(params).then(({ msg }) => {
    ElMessage.success(msg)
    handleClose()
    emit('getList')
  })
}

// 原因
const refusalReasonList = ref([])

const init = async () => {
  getDictData('refusal_reason')
}
</script>

<style scoped lang="scss"></style>
